package com.app.couponsharelife.ui.adapter

import android.graphics.Paint
import androidx.viewpager.widget.PagerAdapter
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.TaobaoGoodBean
import com.app.couponsharelife.ui.listener.OnClickListener
import com.app.couponsharelife.utils.AppUtils
import java.text.DecimalFormat

class ClipViewPagerAdapter(val mViewPager: ViewPager?, val list: List<Any>, val type: Int, val listener: OnClickListener) : PagerAdapter() {

    override fun isViewFromObject(p0: View, p1: Any): Boolean {
        return p0 == p1
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = when (type) {
            0 -> LayoutInflater.from(container.context)
                    .inflate(R.layout.share_invite_pic_item, container, false)
            1 -> LayoutInflater.from(container.context)
                    .inflate(R.layout.home_list_item, container, false)
            2 -> LayoutInflater.from(container.context)
                    .inflate(R.layout.fra_tribe_item, container, false)
            else -> LayoutInflater.from(container.context)
                    .inflate(R.layout.share_invite_pic_item, container, false)
        }
        when (type) {
            1 -> {
                val df = DecimalFormat("#0.0")
                val good = list[position] as TaobaoGoodBean
                view.findViewById<TextView>(R.id.tvGoodPlatform).text = if (good.user_type == "0") "淘宝" else "天猫"
                view.findViewById<TextView>(R.id.tvGoodName).text = good.title
                view.findViewById<TextView>(R.id.tvCouponPrice).text = df.format(good.final_price.toDouble())
                view.findViewById<TextView>(R.id.tvOriginalPrice).text = df.format(good.original_price.toDouble())
                view.findViewById<TextView>(R.id.tvOriginalPrice).paintFlags = view.findViewById<TextView>(R.id.tvOriginalPrice).paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                Glide.with(view).asBitmap()
                        .apply(RequestOptions().placeholder(R.drawable.ic_good_place_holder))
                        .load(good.pict_url)
                        .into(view.findViewById(R.id.ivGoodImage))
            }
            0 -> {
                Glide.with(view).asBitmap()
                        .apply(RequestOptions().centerCrop())
                        .load(list[position])
                        .into(view.findViewById(R.id.ivSharePic))
            }
        }
        view.tag = position

        if (type == 1)
            view.setOnTouchListener { v, event ->
                when (event.action) {
                    MotionEvent.ACTION_UP -> {
                        calculateClickPosition(event.rawX, v)
                    }
                }
                return@setOnTouchListener true
            }
        container.addView(view)
        return view
    }

    private fun calculateClickPosition(rawX: Float, view: View) {
        if (mViewPager != null) {
            val widthPixels = AppUtils.getDisplayMetrics(view.context).widthPixels
            val centerViewWidth = view.width
            val otherViewWidth = (centerViewWidth * 0.9).toInt()
            val margin = (widthPixels - centerViewWidth - 2 * otherViewWidth) / 2
            val centerPosition = mViewPager.currentItem
            val resultPosition = when {
                rawX < otherViewWidth + margin -> centerPosition - 1
                rawX < otherViewWidth + centerViewWidth + margin -> centerPosition
                else -> centerPosition + 1
            }
            if (resultPosition >= 0 && resultPosition < list.size)
                listener.click(resultPosition, list[resultPosition])
        }
    }

    override fun destroyItem(container: ViewGroup, position: Int, any: Any) {
        container.removeView(any as View)
    }
}